<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
import { type CreateOrUpdateTableRequestData, type GetTableData } from "@/api/table/types/table"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Upload, RefreshRight, Edit } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { type RouteRecordName, type RouteRecordRaw, useRouter } from "vue-router"
const router = useRouter()
function goto(path: string) {
  router.push({ path })
}
defineOptions({
  // 命名当前组件
  name: "ElementPlus"
})
function dan() {
  router.push('/labels/add')
  ElMessage.info('选择数量 收货地址、审批人 输入框')
  ElMessage.info('选择数量 收货地址、审批人 输入框')
}
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const tableData2 = ref([
  {
    email: '1',
    email1: '1#仓库',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '小件仓库、配套库',
    email6: '小长兴',
  },
  {
    email: '2',
    email1: '2#仓库',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '轴承仓库、物资库',
    email6: '300亩',
  },
  {
    email: '3',
    email1: '3#仓库',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '物资库',
    email6: '1000亩',
  },
  {
    email: '4',
    email1: '涂装1#仓库',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '涂装仓库',
    email6: '1000亩',
  },
  {
    email: '5',
    email1: '涂装2#仓库',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '涂装仓库',
    email6: '1000亩',
  },
  {
    email: '6',
    email1: '钢构加工部',
    email3: '轴承',
    email2: '张天易',
    email4: '13722128888',
    email5: '钢构仓库',
    email6: '1000亩',
  },
])
//#region 增
const DEFAULT_FORM_DATA: CreateOrUpdateTableRequestData = {
  id: undefined,
  username: "",
  password: ""
}
const dialogVisible = ref<boolean>(false)
const materialDialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<CreateOrUpdateTableRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<CreateOrUpdateTableRequestData> = {
  username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const api = formData.value.id === undefined ? createTableDataApi : updateTableDataApi
    api(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
        dialogVisible.value = false
        getTableData()
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = JSON.parse(JSON.stringify(DEFAULT_FORM_DATA))
}
//#endregion

//#region 删
const handleDelete = (row: GetTableData) => {
  ElMessageBox.confirm(`跳转到新建领料单界面，带入信息`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
  })
}
//#endregion

//#region 改
const handleUpdate = (row: GetTableData) => {
  dialogVisible.value = true
  formData.value = JSON.parse(JSON.stringify(row))
}
const materialDetail = (row: GetTableData) => {
  materialDialogVisible.value = true
  formData.value = JSON.parse(JSON.stringify(row))
}
//#endregion

//#region 查
const tableData = ref([
  {
    email: '1',
    email1: '4784949838',
    email2: '悬臂梁',
    email3: '螺丝',
    email4: '#5#6',
    email5: 'GYD-3',
    email6: '1/2',
    email7: '5',
    email8: 'A-011',
    email9: '2024-04-01 09:35:45',
    email10: '00238372',
    email11: '涂装',
    email12: '1#库',
  },
  {
    email: '2',
    email1: '4784949838',
    email2: '悬臂梁',
    email3: '螺丝',
    email4: '#5#6',
    email5: 'GYD-3',
    email6: '1/2',
    email7: '5',
    email8: 'A-011',
    email9: '2024-04-01 09:35:45',
    email10: '00238371',
    email11: '钢构',
    email12: '1#库',
  },
  {
    email: '3',
    email1: '4784949838',
    email2: '悬臂梁',
    email3: '螺丝',
    email4: '#5#6',
    email5: 'GYD-3',
    email6: '1/2',
    email7: '5',
    email8: 'A-011',
    email9: '2024-04-01 09:35:45',
    email10: '00238376',
    email11: '涂装',
    email12: '1#库',
  }
])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  username: "",
  phone: ""
})
const getTableData = () => {
  loading.value = true
  getTableDataApi({
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    username: searchData.username || undefined,
    phone: searchData.phone || undefined
  })
    .then(({ data }) => {
      paginationData.total = data.total
      // tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion
const activeName = 'first1'
/** 监听分页参数的变化 */
// watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
  <el-card>
    <!-- <el-tabs v-model="activeName">
      <el-tab-pane label="1#库" name="first1">标准件仓库，库位 300亩</el-tab-pane>
      <el-tab-pane label="2#库" name="first2">小件仓库，库位 200亩</el-tab-pane>
      <el-tab-pane label="3#库" name="first3">库位 1000亩</el-tab-pane>
      <el-tab-pane label="涂装 1#库" name="first4">介绍</el-tab-pane>
      <el-tab-pane label="涂装 2#库" name="first5">介绍</el-tab-pane>
      <el-tab-pane label="钢构加工部" name="first6">慧表，本系统数量不做修改</el-tab-pane>
    </el-tabs> -->
    <el-table :data="tableData2">
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column type="index" label="序" align="center" />
          <el-table-column prop="email1" label="仓库名称" align="center" />
          <el-table-column prop="email3" label="物资类型" align="center" />
          <el-table-column prop="email2" label="库长" align="center" />
          <el-table-column prop="email4" label="电话" align="center" />
          <el-table-column prop="email5" label="简介" align="center" />
          <el-table-column prop="email6" label="仓库位置" align="center" />
          <!-- <el-table-column prop="email7" label="照片" align="center">
            <img src="/dabao.png" class="max-w-10" alt="">
          </el-table-column> -->
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <el-button type="primary" text bg size="small" @click="goto('/labels/warehouseDetail')">查看</el-button>
          </el-table-column>
        </el-table>
  </el-card>
    <el-card v-loading="loading" shadow="never" class="my-5">
      <el-form ref="searchFormRef" :inline="true" :model="searchData" label-width="80px">
        <el-form-item prop="username" label="产品工号">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item prop="username" label="产品名称">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          /> </el-form-item
        ><el-form-item prop="username" label="物料名称">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          /> </el-form-item
        ><el-form-item prop="username" label="机号">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item prop="username" label="图号">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          /> </el-form-item
        ><el-form-item prop="username" label="件号">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item prop="username" label="仓库">
          <el-select
            style="width: 150px"
            v-model="searchData.username"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="mb-2">
        <el-button type="primary">批量加入购物车</el-button>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="email" label="序" width="50" align="center" />
          <el-table-column prop="email1" label="产品工号" align="center" />
          <el-table-column prop="email2" label="产品名称" align="center" />
          <el-table-column prop="email3" label="物料名称" align="center" />
          <el-table-column prop="email4" label="机号" align="center" />
          <el-table-column prop="email5" label="图号" align="center" />
          <el-table-column prop="email6" label="件号" align="center" />
          <el-table-column prop="email7" label="实际数量" align="center" />
          <el-table-column prop="email12" label="仓库" align="center" />
          <el-table-column prop="email8" label="库位" align="center" />
          <el-table-column prop="email11" label="发货单位" align="center" />
          <!-- <el-table-column prop="email12" label="结余" align="center" /> -->
          <el-table-column prop="email10" label="入库单号" align="center" />
          <el-table-column prop="email9" label="入库日期" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button
                type="primary"
                text
                bg
                size="small"
                @click="materialDetail(scope.row)"
                >查看</el-button
              >
              <el-button
                type="primary"
                text
                bg
                size="small"
                @click="handleUpdate(scope.row)"
                >加入购物车</el-button
              >
              <el-button
                type="primary"
                text
                bg
                size="small"
                @click="dan"
                >直接下单</el-button
              >
              <!-- @click="handleDelete(scope.row)" -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog v-model="materialDialogVisible" width="30%" title="物料详情" center>
      <!-- <div class="p-2">
        入库日期：2024-04-01 09:34:33
      </div> -->
      <div class="flex items-center flex-col">
        <img class="w-60" src="/xuanbiliang.jpg" alt="" />
        <div>这是一段描述描述悬臂梁的描述</div>
      </div>
      <div class="pt-3 text-right">
        <el-button type="primary">更新照片</el-button>
      </div>
    </el-dialog>
    <!-- 新增/修改 -->
    <el-dialog
      v-model="dialogVisible"
      :title="formData.id === undefined ? '新增仓库' : '修改仓库'"
      @closed="resetForm"
      width="30%"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item prop="username" label="产品工号">
          <el-input v-model="formData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="产品名称" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="物料名称" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="机号" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="图号" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="件号" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="数量" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="库位" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading"
          >确认</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
